package li.webqq.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import li.webqq.api.CheckRequest;
import li.webqq.api.CheckResponse;
import li.webqq.util.Logs;

import org.apache.log4j.Logger;

public class LoginView implements Runnable {
    private static final Logger log = Logs.get();

    final JTextField usernameField = new JTextField();

    final JPasswordField passwordField = new JPasswordField();

    final JButton loginButton = new JButton("登陆");

    String username = "";

    FocusAdapter usernameOnFocusLost = new FocusAdapter() {
        public void focusLost(FocusEvent e) {
            log.info("usernameField focusLost");

            String _username = usernameField.getText();
            if (!username.equals(_username)) {// 发生变化时
                log.info("username changed " + username + " > " + _username);

                CheckRequest request = new CheckRequest();
                request.setUin(username);
                CheckResponse response = (CheckResponse) request.execute(Container.getCookieStore());

                System.err.println(response.getBody());
                username = _username;
            } else {
                log.info("username not changed " + username);
            }
        }
    };

    ActionListener loginButtonClick = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            log.info("loginButtonClick ");

            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());

            System.err.println(username + "&" + password);
        }
    };

    public void initView() {
        JFrame frame = new JFrame("QQ");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setBounds(100, 100, 1000, 680);

        frame.setLayout(null);

        JLabel usernameLabel = new JLabel("QQ:");
        usernameLabel.setBounds(200, 300, 40, 30);
        frame.add(usernameLabel);

        usernameField.setBounds(300, 300, 100, 30);
        usernameField.requestFocus();
        frame.add(usernameField);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(400, 300, 40, 30);
        frame.add(passwordLabel);

        passwordField.setBounds(500, 300, 100, 30);
        passwordField.setVisible(true);
        frame.add(passwordField);

        loginButton.setBounds(600, 300, 100, 30);
        frame.add(loginButton);

        usernameField.requestFocusInWindow();
    }

    public void run() {
        initView();
        usernameField.addFocusListener(usernameOnFocusLost);
        loginButton.addActionListener(loginButtonClick);
    }
}